/**
 * index
 * @authors Your Name (you@example.org)
 * @date    2017-02-14 18:34:07
 * @version $Id$
 */
// fetch polyfill 假设你已经使用了 Promise 的 polyfill。
// 确保你使用 Promise polyfill 的一个最简单的办法是在所有应用代码前启用 Babel 的 ES6 polyfill
import 'babel-polyfill'
import React from 'react'
import ReactDOM from 'react-dom'
import {
	createStore,
	applyMiddleware,
	compose
} from 'redux'
import {
	Provider
} from 'react-redux'
import thunkMiddleware from 'redux-thunk'
import createLogger from 'redux-logger'
import {
	Router,
	Route,
	IndexRoute,
	hashHistory,
	browserHistory,
	createRoutes
} from 'react-router'

import reducerCollection from './app/reducer.js'
import rawRoutes from './app/routers.js'

// 管理api接口
import __API__ from './app/config.js'
window.__API__ = __API__

const mixStoreToRoutes = (routes) => {
	return routes && routes.map(route => {
		// console.log(route)
		return Object.assign({}, route, {
			childRoutes: mixStoreToRoutes(route.childRoutes),
			onEnter: route.onEnter && function(props, replaceState, cb) {
				route.onEnter(store.dispatch, props)
				cb && cb()
			}
		})
	});
}

// 创建store
// 调试插件
// https://github.com/zalmoxisus/redux-devtools-extension
// const loggerMiddleware = createLogger()
// const reducer = combineReducers({
// })
// const store = createStore(
// 	reducer,
// 	applyMiddleware(
// 		thunkMiddleware, // 允许我们 dispatch() 函数
// 		loggerMiddleware // 一个很便捷的 middleware，用来打印 action 日志
// 	)
// )
const loggerMiddleware = createLogger()
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const store = createStore(reducerCollection, /* preloadedState, */ composeEnhancers(
	applyMiddleware(thunkMiddleware, loggerMiddleware)
));
const routes = mixStoreToRoutes(createRoutes(rawRoutes));

ReactDOM.render(
	<Provider store={store}>
     	<Router history={hashHistory} routes={routes}/>
  	</Provider>,
	document.getElementById('root')
)